/**
 * 主页控制器
 */


const Base = require('./base');
module.exports = class extends Base
{

    constructor(ctx)
    {
        super(ctx);
        this._workGuid = this._getGuid();
        this._CPInstance = this.model('v2/competition');
    }


    async _getCP()
    {
        this.finishedTotal = await this._CPInstance.getFinishedTotal();
        this.nowItems = await this._CPInstance.getNowItems();
        this.finishedItems = await  this._CPInstance.getFinishedItems();
        this.nowTotal = await this._CPInstance.getNowTotal();
        return {
            'nowCompetition': {
                'total': this.nowTotal,
                'items': this.nowItems
            },
            'oldCompetition': {
                'total': this.finishedTotal,
                'items': this.finishedItems
            }
        };
    }


    async _getCPWorksCountByWorksGuid()
    {
        let count=await think.service('v2/home').getCPWorksCountByWorksGuid(this._workGuid);
       return{
           'worksTotal':count
       }
    }

    /**
     * rest get请求
     * @returns {Promise.<*>}
     */
    async getAction()
    {
        if (this._workGuid)
        {
            return this.success(await this._getCPWorksCountByWorksGuid());
        } else
        {
            return this.success(await this._getCP());
        }

    }


    _getGuid()
    {
        const workGuid = this.get('workGuid');
        if (workGuid && (think.isString(workGuid)))
        {
            return workGuid;
        }
        return false;
    }
};
